package com.zmn.oms.business.impl.nonstandard;

import com.google.common.collect.Lists;
import com.zmn.common.utils.collection.CollectionUtil;
import com.zmn.common.utils.reflect.BeanMapper;
import com.zmn.oms.business.interfaces.nonstandard.OrderNonstandardBService;
import com.zmn.oms.common.annotation.ZsNormalOrderWorkLog;
import com.zmn.oms.common.constant.OrderLogConsts;
import com.zmn.oms.model.dto.work.modify.OrderNonstandardDTO;
import com.zmn.oms.model.entity.nonstandard.OrderNonstandard;
import com.zmn.oms.services.interfaces.nonstandard.OrderNonstandardService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author ：SongWeidong
 * @date ：2020/8/5 19:17
 * @description：非标信息
 */
@Service
public class OrderNonstandardBServiceImpl implements OrderNonstandardBService {

    @Resource
    private OrderNonstandardService orderNonstandardService;

    @Override
    @ZsNormalOrderWorkLog(type = OrderLogConsts.ORDER_LOG_TYPE_NONSTANDARD, beforeProceed = false)
    public void saveOrderNonstandardList(OrderNonstandardDTO orderNonstandardDTO) {

        List<OrderNonstandard> orderNonstandards = orderNonstandardService.listNonstandardByOrderId(orderNonstandardDTO.getOrderId());

        // 处理日志
        // 新增/删除 日志
        List<OrderNonstandard> newOrderNonstandards = Lists.newArrayList();
        List<OrderNonstandard> oldOrderNonstandards = Lists.newArrayList();
        List<OrderNonstandard> addOrderNonstandards = Lists.newArrayList();
        List<OrderNonstandard> delOrderNonstandards = Lists.newArrayList();
        int imgFlag = 0;
        Map<Long, OrderNonstandard> nonstandardMap = orderNonstandards.stream().collect(Collectors.toMap(OrderNonstandard::getNonstandardId, OrderNonstandard -> OrderNonstandard));
        List<OrderNonstandard> newNonstandardList = orderNonstandardDTO.getOrderNonstandardList().stream().filter(e -> Objects.isNull(e.getNonstandardId())).collect(Collectors.toList());
        Map<Long, OrderNonstandard> nonstandardMapDTO = orderNonstandardDTO.getOrderNonstandardList().stream().filter(e -> Objects.nonNull(e.getNonstandardId())).collect(Collectors.toMap(OrderNonstandard::getNonstandardId, OrderNonstandard -> OrderNonstandard));
        List<Long> idList = orderNonstandards.stream().filter(e -> Objects.equals(e.getType(), 1)).map(OrderNonstandard::getNonstandardId).collect(Collectors.toList());
        List<Long> dtoIdList = orderNonstandardDTO.getOrderNonstandardList().stream().filter(e -> Objects.nonNull(e.getNonstandardId()) && Objects.equals(e.getType(), 1)).map(OrderNonstandard::getNonstandardId).collect(Collectors.toList());
        if (CollectionUtil.isNotNullOrEmpty(orderNonstandards)) {
            dtoIdList.forEach(e -> {
                if (idList.contains(e) && (!nonstandardMap.get(e).getTitle().equals(nonstandardMapDTO.get(e).getTitle())
                        || !nonstandardMap.get(e).getContent().equals(nonstandardMapDTO.get(e).getContent()))) {
                    // 修改
                    oldOrderNonstandards.add(nonstandardMap.get(e));
                    newOrderNonstandards.add(nonstandardMapDTO.get(e));
                }
            });
            idList.forEach(e -> {
                if (!dtoIdList.contains(e)) {
                    // 删除
                    delOrderNonstandards.add(nonstandardMap.get(e));
                }
            });
            String oldImageSrc = "";
            String newImgStr = Optional.ofNullable(orderNonstandardDTO.getImgStr()).orElse("");
            List<OrderNonstandard> imgList = orderNonstandards.stream().filter(e -> Objects.equals(e.getType(), 2)).collect(Collectors.toList());
            if(CollectionUtil.isNotNullOrEmpty(imgList)){
                OrderNonstandard imgNonstandard = Optional.ofNullable(imgList.get(0)).orElse(new OrderNonstandard());
                oldImageSrc = imgNonstandard.getImageSrc();
            }
            if (!oldImageSrc.equals(newImgStr)) {
                if (newImgStr.length() == oldImageSrc.length()) {
                    // 修改
                    imgFlag = 1;
                } else if (newImgStr.length() < oldImageSrc.length()) {
                    // 删除
                    imgFlag = 2;
                } else {
                    // 新增
                    imgFlag = 3;
                }
            }
        } else {
            if (StringUtils.isNotBlank(orderNonstandardDTO.getImgStr())) {
                imgFlag = 3;
            }
        }
        // 新增
        addOrderNonstandards.addAll(newNonstandardList);
        orderNonstandardDTO.setOldOrderNonstandardList(oldOrderNonstandards);
        orderNonstandardDTO.setNewOrderNonstandardList(newOrderNonstandards);
        orderNonstandardDTO.setAddOrderNonstandardList(addOrderNonstandards);
        orderNonstandardDTO.setDelOrderNonstandardList(delOrderNonstandards);
        if (CollectionUtils.isNotEmpty(orderNonstandards)) {
            orderNonstandardService.deleteByOrderId(orderNonstandardDTO.getOrderId());
        }
        // 处理要保存的数据
        List<OrderNonstandard> orderNonstandardList = BeanMapper.mapList(orderNonstandardDTO.getOrderNonstandardList(), OrderNonstandard.class);
        orderNonstandardList.forEach(e -> {
            e.setOrderId(orderNonstandardDTO.getOrderId());
            e.setType(1);
            e.setCreater(orderNonstandardDTO.getOperator());
        });
        if (StringUtils.isNotBlank(orderNonstandardDTO.getImgStr())) {
            OrderNonstandard orderNonstandard = OrderNonstandard.builder().creater(orderNonstandardDTO.getOperator())
                    .orderId(orderNonstandardDTO.getOrderId()).type(2).imageSrc(orderNonstandardDTO.getImgStr()).build();
            orderNonstandardList.add(orderNonstandard);
        }
        orderNonstandardDTO.setImgFlag(imgFlag);
        if (CollectionUtils.isNotEmpty(orderNonstandardList)) {
            orderNonstandardService.insertNonstandardList(orderNonstandardList);
        }
    }
}
